package com.example.yin.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Path;
import java.nio.file.Paths;

@RestController
@RequestMapping("/user01")
@ConditionalOnProperty(name = "minio.enabled", havingValue = "false")
public class FileController {

    @Value("${file.upload.path:./uploads}")
    private String uploadPath;

    //获取歌曲
    @GetMapping("/{fileName:.+}")
    public ResponseEntity<Resource> getMusic(@PathVariable String fileName) {
        try {
            Path file = Paths.get(uploadPath).resolve(fileName);
            Resource resource = new UrlResource(file.toUri());
            
            if (resource.exists() || resource.isReadable()) {
                return ResponseEntity.ok()
                        .contentType(MediaType.APPLICATION_OCTET_STREAM)
                        .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
                        .body(resource);
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (MalformedURLException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
    
    //获取歌手图片 - 支持多种路径格式
    @GetMapping("/singer/img/{fileName:.+}")
    public ResponseEntity<Resource> getSingerImage(@PathVariable String fileName) {
        // 尝试多种可能的路径
        String[] possiblePaths = {
            "singer/img/" + fileName,
            "img/singerPic/" + fileName,
            "singerPic/" + fileName
        };
        
        for (String path : possiblePaths) {
            ResponseEntity<Resource> response = getImageFile(path);
            if (response.getStatusCode() != HttpStatus.NOT_FOUND) {
                return response;
            }
        }
        
        return ResponseEntity.notFound().build();
    }
    
    //获取歌单图片
    @GetMapping("/songlist/{fileName:.+}")
    public ResponseEntity<Resource> getSongListImage(@PathVariable String fileName) {
        return getImageFile("songlist/" + fileName);
    }
    
    //获取歌的图片
    @GetMapping("/img/songPic/{fileName:.+}")
    public ResponseEntity<Resource> getSongImage(@PathVariable String fileName) {
        return getImageFile("img/songPic/" + fileName);
    }
    
    //获取头像
    @GetMapping("/img/avatorImages/{fileName:.+}")
    public ResponseEntity<Resource> getAvatarImage(@PathVariable String fileName) {
        return getImageFile("img/avatorImages/" + fileName);
    }
    
    //获取轮播图
    @GetMapping("/img/swiper/{fileName:.+}")
    public ResponseEntity<Resource> getSwiperImage(@PathVariable String fileName) {
        return getImageFile("img/swiper/" + fileName);
    }
    
    //获取歌手图片 - 支持数据库中的原始路径格式
    @GetMapping("/img/singerPic/{fileName:.+}")
    public ResponseEntity<Resource> getSingerPicImage(@PathVariable String fileName) {
        return getImageFile("img/singerPic/" + fileName);
    }
    
    private ResponseEntity<Resource> getImageFile(String filePath) {
        try {
            Path file = Paths.get(uploadPath).resolve(filePath);
            Resource resource = new UrlResource(file.toUri());
            
            if (resource.exists() || resource.isReadable()) {
                return ResponseEntity.ok()
                        .contentType(MediaType.IMAGE_JPEG)
                        .body(resource);
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (MalformedURLException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
}

